﻿(function () {
    $(function () {
        var _$weChatUsersTable = $("#WeChatUsersTable");
        var _weChatUserService = abp.services.weChat.weChatUser;

        var _permissions = {
            create: abp.auth.hasPermission("WeChatPermissions.Roots.Tenants.WeChatUser.Create"),
            edit: abp.auth.hasPermission("WeChatPermissions.Roots.Tenants.WeChatUser.Edit"),
            delete: abp.auth.hasPermission("WeChatPermissions.Roots.Tenants.WeChatUser.Delete")
        };

        var _createOrEditModal = new app.ModalManager({
            viewUrl: abp.appPath + "WeChat/WeChatUsers/CreateOrEditModal",
            scriptUrl: abp.appPath +
                "PlugIns/Magicodes.WeChat.Web.Mvc/wwwroot/view-resources/Views/WeChatUsers/_CreateOrEditModal.js",
            modalClass: "CreateOrEditWeChatUserModal"
        });

        _$weChatUsersTable.jtable({
            title: l("WeChatUser"),
            paging: true,
            sorting: true,
            multiSorting: true,

            actions: {
                listAction: {
                    method: _weChatUserService.getWeChatUsers
                }
            },

            fields: {
                id: {
                    key: true,
                    list: false
                },
                actions: {
                    title: app.localize("Actions"),
                    width: "6%",
                    display: function (data) {
                        var $span = $("<span></span>");

                        if (_permissions.edit) {
                            $('<button class="btn btn-default btn-xs" title="' +
                                    app.localize("Edit") +
                                    '"><i class="fa fa-edit"></i></button>')
                                .appendTo($span)
                                .click(function () {
                                    _createOrEditModal.open({ id: data.record.id });
                                });
                        }

                        if (_permissions.delete) {
                            $('<button class="btn btn-default btn-xs" title="' +
                                    app.localize("Delete") +
                                    '"><i class="fa fa-trash-o"></i></button>')
                                .appendTo($span)
                                .click(function () {
                                    deleteWeChatUser(data.record);
                                });
                        }

                        return $span;
                    }
                },
                headImgUrl: {
                    title: l("HeadImgUrl"),
                    width: "6%",
                    display: function (data) {
                        var $el = $('<div class="mt-element-overlay" style="height:160px;width:160px;"></div>');
                        if (data.record.headImgUrl) {
                            $el.append(
                                '<div class="row">'+
                                '<div class="col-md-12">'+
                                '<span class="mt-overlay-6">' +
                                '<img class="item-pic"   src="' + data.record.headImgUrl + '">' +
                                '<div class="mt-overlay">' +
                                '<h2>'+data.record.nickName+'</h2>' +
                                '<p>' +
                                //'<a class="mt-info uppercase btn default btn-outline" href="#">Learn More</a>' +
                                '</p>' +
                                '</div>' +
                                '</span>'+
                                '</div>'+
                                '</div>'
                            );
                        }
                        return $el;
                    }
                },
                nickName: {
                    title: l("NickName"),
                    width: "6%",
                    display: function (data) {
                        var $span = $('<span></span>');
                        if (data.record.nickName) {
                            $span.append('<p>' + data.record.nickName + "</p>");
                        }
                        if (data.record.city) {
                            $span.append('<p>' + data.record.country + data.record.province + data.record.city + "</p>");
                        }
                        if (data.record.language) {
                            $span.append('<p>' + data.record.language + "</p>");
                        }
                        //1:男 2：女 其他：未知
                        if (data.record.sex === 1) {
                            $span.append('<p><span class="badge bg-blue-sharp"><i class="fa fa-mars bg-font-red bold"></i></span></p>');
                        } else if (data.record.sex === 2) {
                            $span.append('<p><span class="badge bg-purple-wisteria"><i class="fa fa-venus bg-font-red bold"></i></span></p>');
                        } else {
                            $span.append('<p><span class="badge bg-green-turquoise"><i class="fa fa-neuter bg-font-red bold"></i></span></p>');
                        }
                        if (data.record.subscribe) {
                            $span.append('<p>'+l("Subscribe")+':&nbsp;<span class="label label-success">' + app.localize("Yes") + "</span>&nbsp;</p>");
                        } else {
                            $span.append('<p>'+l("Subscribe")+':&nbsp;<span class="label label-info">' + app.localize("No") + "</span>&nbsp;</p>");
                        }
                        return $span;
                    }
                },
                //city: {
                //    title: l("City"),
                //    width: "6%"
                //},
                //country: {
                //    title: l("Country"),
                //    width: "6%"
                //},
                //province: {
                //    title: l("Province"),
                //    width: "6%"
                //},
                //language: {
                //    title: l("Language"),
                //    width: "6%"
                //},
                subscribeTime: {
                    title: l("SubscribeTime"),
                    width: "6%",
                    display: function (data) {
                        return moment(data.record.subscribeTime).format("L");
                    }
                },
                remark: {
                    title: l("Remark"),
                    width: "6%"
                },
                //sex: {
                //    title: l("Sex"),
                //    width: "6%"
                //},
                groupId: {
                    title: l("GroupId"),
                    width: "6%"
                },
                creationTime: {
                    title: l("CreationTime"),
                    width: "6%",
                    display: function (data) {
                        return moment(data.record.creationTime).format("L");
                    }
                }
            }

        });

        function deleteWeChatUser(weChatUser) {
            var tip = app.localize("weChatUserDeleteWarningMessage", "WeChat");
            if (tip === "weChatUserDeleteWarningMessage") tip = "确定删除此项？"; //TODO:修改为从默认源获取   
            abp.message.confirm(
                tip,
                function (isConfirmed) {
                    if (isConfirmed) {
                        _weChatUserService.deleteWeChatUser({
                            id: weChatUser.id
                        }).done(function () {
                            getWeChatUsers();
                            abp.notify.success(app.localize("SuccessfullyDeleted"));
                        });
                    }
                }
            );
        };

        function l(name) {
            var str = abp.localization.localize(name, "WeChat");
            if (str === name) return app.localize(name);
            return str;
        }

        $("#CreateNewWeChatUserButton").click(function () {
            _createOrEditModal.open();
        });

        function getWeChatUsers() {
            _$weChatUsersTable.jtable("load");
        }

        abp.event.on("app.createOrEditWeChatUserModalSaved",
            function () {
                getWeChatUsers();
            });

        getWeChatUsers();
    });
})();